<?php
//模块插件类
//插件功能类，由系统调用，实现具体功能。
defined('BY_JYA') or exit('error');
class core_jy_h5game extends plugin{
	public function payresult($message){
		$order=$this->get('',array('uniacid'=>$message['uniacid'],'id'=>$message['oid']),array('fee'));
		if($order['fee']==$message['fee']){
			$this->update('',array('transaction_id'=>$message['transaction_id']),array('id'=>$message['oid']));
		}else{
			$this->update('',array('transaction_id'=>'校验失败：实付金额：'.$message['fee']),array('id'=>$message['oid']));
		}
	}
	public function receive($message){
		if($message['Event']=='subscribe'){
			//这里编写关注事件处理代码
			
		}
		if($message['Event']=='unsubscribe'){
			//这里编写取消关注事件处理代码
			
		}
	}
	function get_rand($award) {
			foreach ($award as $key => $val){
				  $arr[$key] = $val['rate']; 
			}
			//概率数组的总概率精度 
			$proSum = 100; 
			//概率数组循环
			$luckkey=100;
			foreach ($arr as $key => $v) {
				$randNum = mt_rand(1, $proSum); 
				if ($randNum <= $v) {
				  $luckkey = $key; 
				  break; 
				} else { 
				  $proSum -= $v; 
				}    
			}
		return $luckkey;
	}
	function sendhongbaoto($arr) {
		global $_AR,$_RQ;
		$data['mch_id'] = $arr['mchid'];
		$data['mch_billno'] = $data['mch_id'].date("Ymd",time()).date("His",time()).rand(1111,9999);
		$data['nonce_str'] = $this->createNoncestr();
		$data['re_openid'] = $arr['openid'];
		$data['wxappid'] = $arr['wxappid'];
	    $data['nick_name'] = $arr['hbname'];
		$data['send_name'] = $_SESSION['account']['title'];
	 
		$data['total_amount'] = $arr['fee']*100;
		$data['min_value'] = $arr['fee']*100;
		$data['max_value'] = $arr['fee']*100;
		$data['total_num'] = 1;
		$data['client_ip'] = $_SERVER['REMOTE_ADDR'];
		$data['act_name'] = $arr['hbname'];
		$data['remark'] = '感谢您对我们的支持。';
		$data['wishing'] = $arr['body'];
		
		if(!$data['re_openid']) 
		{
			$rearr['return_msg']='缺少用户openid';
			return $rearr;
		}
		$data['sign'] = $this->getSign($data);
		
		$xml = $this->arrayToXml($data);
		$url ="https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
		$re = $this->wxHttpsRequestPem($xml,$url);
		$rearr = $this->xmlToArray($re);
		//exi($re,'error');
		return $rearr;
	}
	function createNoncestr( $length = 32 ) {
		$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
		$str ="";
		for ( $i = 0; $i < $length; $i++ ) 
		{
			$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
		}
		return $str;
	}
	function getSign($Obj) {
		foreach ($Obj as $k => $v) 
		{
			$Parameters[$k] = $v;
		}
		ksort($Parameters);
		$String = $this->formatBizQueryParaMap($Parameters, false);
		$setting = uni_setting();
		$String = $String."&key=".$setting['pay']['wechat_miyue'];
	//	exi($setting['pay']['wechat_miyue'],'error');
		$String = md5($String);
		$result_ = strtoupper($String);
		return $result_;
	}
	function formatBizQueryParaMap($paraMap, $urlencode) {
		$buff = "";
		ksort($paraMap);
		foreach ($paraMap as $k => $v) 
		{
			if($urlencode) 
			{
				$v = urlencode($v);
			}
			$buff .= $k . "=" . $v . "&";
		}
		$reqPar;
		if (strlen($buff) > 0) 
		{
			$reqPar = substr($buff, 0, strlen($buff)-1);
		}
		return $reqPar;
	}
	function arrayToXml($arr) {
		$xml = "<xml>";
		foreach ($arr as $key=>$val) 
		{
			if (is_numeric($val)) 
			{
				$xml.="<".$key.">".$val."</".$key.">";
			}
			else $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
		}
		$xml.="</xml>";
		return $xml;
	}
	function wxHttpsRequestPem( $vars,$url, $second=30,$aHeader=array()) {
		//global $_W;
		$ch = curl_init();		
		curl_setopt($ch,CURLOPT_TIMEOUT,$second);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
		
		curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
		curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/addons/jy_h5game/cert/'.$_SESSION['uniacid'].'/apiclient_cert.pem');
		curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
		curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/addons/jy_h5game/cert/'.$_SESSION['uniacid'].'/apiclient_key.pem');

		if( count($aHeader) >= 1 )
		{
			curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
		}
		curl_setopt($ch,CURLOPT_POST, 1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
		$data = curl_exec($ch);
		//exi($data,'error');
		if($data)
		{
			curl_close($ch);
			return $data;
		}
		else 
		{
			$error = curl_errno($ch);
			echo "call faild, errorCode:$error\n";
			curl_close($ch);
			return false;
		}
	} 
	function xmlToArray($xml) {
		$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
		return $array_data;
	}
	function make_coupon_card() {
		$code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$rand = $code[rand(0,25)]
			.strtoupper(dechex(date('m')))
			.date('d').substr(time(),-5)
			.substr(microtime(),2,5)
			.sprintf('%02d',rand(0,99));
		for(
			$a = md5( $rand, true ),
			$s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
			$d = '',
			$f = 0;
			$f < 8;
			$g = ord( $a[ $f ] ),
			$d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
			$f++
		);
		return $d;
	} 
}
?>
